import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { getType } from "@/api/type";

// 异步获取所有 type 类型
export const getTypeList = createAsyncThunk("type/getTypeList", async () => {
  const response = await getType();
  // 填充返回的数据到状态仓库
  return response.data;
});

const typeSlice = createSlice({
  name: "type",
  initialState: {
    typeList: [],
    issueTypeId: "all",
    bookTypeId: "all",
  },
  reducers: {
    updateIssueTypeId: (state, { payload }) => {
      state.issueTypeId = payload;
    },
    updateBookTypeId: (state, { payload }) => {
      state.bookTypeId = payload;
    },
  },
  // 专门处理异步的 reducer
  extraReducers: (builder) => {
    builder.addCase(getTypeList.fulfilled, (state, { payload }) => {
      state.typeList = payload;
    });
  },
});

export const { updateIssueTypeId, updateBookTypeId } = typeSlice.actions;
export default typeSlice.reducer;
